package com.xiaoniu.imclient;

import android.app.Application;

import com.xiaoniu.imclient.networkModule.MqttNetworkModuleFactory;
import com.xiaoniu.imclient.networkModule.WebSocketNetWorkModuleFactory;
import com.xiaoniu.imclient.networkModule.rong.RongNetworkModuleFactory;
import com.xiaoniu.lib_keleim.ConversationType;
import com.xiaoniu.lib_keleim.utils.HistoryMessageOption;
import com.xiaoniu.lib_keleim.IMClient;
import com.xiaoniu.lib_keleim.Schemes;
import com.xiaoniu.lib_keleim.db.ConversationListCallBack;
import com.xiaoniu.lib_keleim.db.MessageCountCallBack;
import com.xiaoniu.lib_keleim.db.MessageListCallBack;
import com.xiaoniu.lib_keleim.pojo.dos.UserEntity;
import com.xiaoniu.lib_keleim.pojo.vos.BaseMessage;
import com.xiaoniu.imclient.interceptors.DecodeInterceptor;
import com.xiaoniu.imclient.interceptors.GsonConverterFactory;
import com.xiaoniu.imclient.interceptors.HttpLoggingInterceptor;
import com.xiaoniu.lib_keleim.spi.ReceiveMessageListener;

import java.util.HashMap;

public class IMClientTest {
    
    public void initIM(Application application){

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        DecodeInterceptor decodeInterceptor = new DecodeInterceptor();
        //HttpEncodeInterceptor encodeInterceptor = new HttpEncodeInterceptor();

        IMClient.getManager()
                .addNetworkModule(new MqttNetworkModuleFactory())
                .addNetworkModule(new WebSocketNetWorkModuleFactory())
                .addNetworkModule(new RongNetworkModuleFactory())
                .addConverterFactory(new GsonConverterFactory())
                .addInterceptor(loggingInterceptor)
                .addInterceptor(decodeInterceptor)
                //.addInterceptor(decodeInterceptor)
                //.addInterceptor(encodeInterceptor)
                .retryOnConnectionFailure(3)
                .connect(application,new UserEntity());



        HashMap<Integer, Class<?>> msgType = new HashMap<>();
        msgType.put(1,String.class);
        //msgType.put(2,CPMessage.class);
        //msgType.put(3,SystemMessage.class);
        IMClient.getManager().registerMsgType(msgType);
        addOnReceiveMessageListener();
    }


    public void addOnReceiveMessageListener(){
        IMClient.getManager().addOnReceiveMessageListener(new ReceiveMessageListener() {
            @Override
            public void onReceivedMessage(BaseMessage message) {

            }
        });
    }


    public void sendMessage(){
        BaseMessage obtain = BaseMessage.obtain("targetId", ConversationType.CHATROOM, "消息内容");
        IMClient.getManager().sendMessage(obtain);
    }

    public void sendMessage(String roomId){
        IMClient.getManager().joinRoom(roomId);
    }

    public void quitRoom(String roomId){
        IMClient.getManager().quitRoom(roomId);
    }


    public void getMessages(ConversationType conversationType, String targetId, String channelId, MessageListCallBack callBack){
        HistoryMessageOption option = new HistoryMessageOption(1,50);
        //分页查询,先查本地,本地查完了再去查服务端数据
        IMClient.getManager().getMessages(Schemes.LOCALHOST,conversationType,targetId,channelId, option,callBack);
        //IMClient.getManager().getMessages(Schemes.BIZ_SERVER,conversationType,targetId,channelId, option,callBack);
    }

    public void getConversations(ConversationType conversationType, String targetId, ConversationListCallBack callBack){
        IMClient.getManager().getConversations(Schemes.LOCALHOST,conversationType,targetId,callBack);
        //IMClient.getManager().getConversations(Schemes.BIZ_SERVER,conversationType,targetId,callBack);
    }

    public void getUnreadCount(ConversationType conversationType, String targetId, String channelId, MessageCountCallBack callBack){
        IMClient.getManager().getUnreadCount(Schemes.LOCALHOST,conversationType,targetId,channelId,callBack);
    }


}
